//
//  LMSendCOmmentController.m
//  EPA
//
//  Created by lanou3g on 16/4/26.
//  Copyright © 2016年 liman. All rights reserved.
//

#import "LMSendCOmmentController.h"
#import "LMCommentController.h"
@interface LMSendCOmmentController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UIButton *sendBtn;

@end

@implementation LMSendCOmmentController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.sendBtn addTarget:self action:@selector(didSend) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.title  = @"发表评论";
}
- (void)didSend{

        [self didCommentWithConnent:self.textView.text];
        [self.textView resignFirstResponder];
        [self.navigationController popViewControllerAnimated:YES];

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.textView resignFirstResponder];
}
- (void)didCommentWithConnent:(NSString *)connent{
    NSURL *url = [NSURL URLWithString:@"http://api2.pianke.me/comment/add"];
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
    [mRequest setHTTPMethod:@"POST"];
    NSString *bodyStr = [NSString stringWithFormat:@"auth=%@&client=1&content=%@&contentid=%@&deviceid=6D4DD967-5EB2-40E2-A202-37E64F3BEA31&recid=&reuid=&version=3.0.6",[UserInfoManager shareManager].auth, connent,self.connentID];
    [mRequest setHTTPBody:[bodyStr dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:mRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data) {
            NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            if ([jsonDict[@"result"] isEqual:@1]) {
                NSLog(@"评论成功"); 
            }
        }
    }];
    
    [dataTask resume];
}

-(void)viewDidAppear:(BOOL)animated{
    dispatch_async(dispatch_get_main_queue(), ^{
        LMCommentController *commentC = [[LMCommentController alloc]init];
        [commentC didLoadData];
    });
}
@end
